using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ShopAdDal
	{
		public bool Exists(string AdName)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_ShopAd");
			stringBuilder.Append(" where ");
			stringBuilder.Append("AdName = @AdName ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AdName", SqlDbType.NVarChar, 200)
			};
			array[0].Value = AdName;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public void Add(ShopAdModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_ShopAd(");
			stringBuilder.Append("AdType,AdName,AdUrl,AdSrc,AdTitle");
			stringBuilder.Append(") values (");
			stringBuilder.Append("@AdType,@AdName,@AdUrl,@AdSrc,@AdTitle");
			stringBuilder.Append(") ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AdType", SqlDbType.NVarChar, 50),
				new SqlParameter("@AdName", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdUrl", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdSrc", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdTitle", SqlDbType.NVarChar, 200)
			};
			array[0].Value = model.AdType;
			array[1].Value = model.AdName;
			array[2].Value = model.AdUrl;
			array[3].Value = model.AdSrc;
			array[4].Value = model.AdTitle;
			Conn.ExecuteSql(stringBuilder.ToString(), array);
		}
		public bool Update(ShopAdModel model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ShopAd set ");
			stringBuilder.Append(" AdType = @AdType , ");
			stringBuilder.Append(" AdName = @AdName , ");
			stringBuilder.Append(" AdUrl = @AdUrl , ");
			stringBuilder.Append(" AdSrc = @AdSrc , ");
			stringBuilder.Append(" AdTitle = @AdTitle  ");
			stringBuilder.Append(" where AdName=@AdName  ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AdType", SqlDbType.NVarChar, 50),
				new SqlParameter("@AdName", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdUrl", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdSrc", SqlDbType.NVarChar, 200),
				new SqlParameter("@AdTitle", SqlDbType.NVarChar, 200)
			};
			array[0].Value = model.AdType;
			array[1].Value = model.AdName;
			array[2].Value = model.AdUrl;
			array[3].Value = model.AdSrc;
			array[4].Value = model.AdTitle;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(string AdName)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ShopAd ");
			stringBuilder.Append(" where AdName=@AdName ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AdName", SqlDbType.NVarChar, 200)
			};
			array[0].Value = AdName;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public ShopAdModel GetModel(string AdName)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select AdType, AdName, AdUrl, AdSrc, AdTitle  ");
			stringBuilder.Append("  from HL_ShopAd ");
			stringBuilder.Append(" where AdName=@AdName ");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AdName", SqlDbType.NVarChar, 200)
			};
			array[0].Value = AdName;
			ShopAdModel shopAdModel = new ShopAdModel();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			ShopAdModel result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				shopAdModel.AdType = dataSet.Tables[0].Rows[0]["AdType"].ToString();
				shopAdModel.AdName = dataSet.Tables[0].Rows[0]["AdName"].ToString();
				shopAdModel.AdUrl = dataSet.Tables[0].Rows[0]["AdUrl"].ToString();
				shopAdModel.AdSrc = dataSet.Tables[0].Rows[0]["AdSrc"].ToString();
				shopAdModel.AdTitle = dataSet.Tables[0].Rows[0]["AdTitle"].ToString();
				result = shopAdModel;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select * ");
			stringBuilder.Append(" FROM HL_ShopAd ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string fieldOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" * ");
			stringBuilder.Append(" FROM HL_ShopAd ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetListRowid(int Top, string strWhere, string fieldOrder, params string[] selectField)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			string text = "*";
			if (selectField.Length > 0)
			{
				text = selectField[0];
				if (text.Trim() == "")
				{
					text = "*";
				}
			}
			stringBuilder.AppendFormat(" ROW_NUMBER() OVER (ORDER BY AdName ASC) rowid,{0} ", text);
			stringBuilder.Append(" FROM HL_ShopAd ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + fieldOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
